//#define __FORCE_TRACE__
//#define __FORCE_DEBUG__
//#define __FORCE_INFO__
#define THIS_FILE "fn_getRespawnLocations.sqf"
#include <btc_macros.h>
TRACE_1("Start",_this);

private ["_dlg_type","_leader","_list_names","_list_objects","_side","_array","_list","_vehicle","_var","_var_name","_display_name","_units","_unit","_i","_marker_name","_marker_text","_side_var","_vehicles"];
DEFAULT_PARAM(_dlg_type,0,0);

_list_names = ["BASE"];
_list_objects = [BTC_respawn_marker];
_side = "";
_array = [];
switch (true) do
{
	case (BTC_side == west) : {_side = "west";_array = +BTC_vehs_mobile_west_str;};
	case (BTC_side == east) : {_side = "east";_array = +BTC_vehs_mobile_east_str;};
	case (BTC_side == resistance) : {_side = "guer";_array = +BTC_vehs_mobile_guer_str;};
	case (BTC_side == civilian) : {_side = "civ";_array = +BTC_vehs_mobile_civ_str;};
};
_side_var = format["BTC_Mobile_%1",_side];
TRACE_1("_side_var",_side_var);
if (_dlg_type in [0]) then {
	for "_i" from 0 to (49) step 1 do {
		_marker_name = format["Respawn_%1",_i];
		if (markerType _marker_name != "") then {
			_marker_text = markerText _marker_name;
			if (_marker_text != "") then {
				_marker_text = localize _marker_text;
				if (_marker_text == "") then {
					_marker_text = markerText _marker_name;
				};
			} else {
				_marker_text = format ["Respawn %1",_i];
			};
			_list_objects = _list_objects + [_marker_name];
			_list_names = _list_names + [_marker_text];
		};
	};
	for "_i" from 0 to (49) step 1 do {
		_marker_name = format["Respawn_%1_%2",_side,_i];
		if (markerType _marker_name != "") then {
			_marker_text = markerText _marker_name;
			if (_marker_text != "") then {
				_marker_text = localize _marker_text;
				if (_marker_text == "") then {
					_marker_text = markerText _marker_name;
				};
			} else {
				_marker_text = format ["Respawn %1 %2",_side,_i];
			};
			_list_objects = _list_objects + [_marker_name];
			_list_names = _list_names + [_marker_text];
		};
	};
};

if (BTC_active_mobile == 1) then {
	TRACE_1("_array",_array);
	if (!isnil "_array" && {typename _array == typename []} && {count _array > 0}) then {
		_vehicles = vehicles + (allMissionObjects "FlagCarrierCore");
		{
			_vehicle = _x;
			_var = "";
			{
				_var = _x;
				_var_name = _vehicle getvariable [_side_var,""];
				TRACE_1("Vehicle",ARR_3(_var,_var_name,_side_var));
				if ((typeName _var_name == typename "") && {_var_name == _var}) exitWith {
					_list_objects = _list_objects + [_vehicle];
					_display_name = _vehicle getVariable ["BTC_mobile_Name_" + _side, _var_name];
					_display_name = format["%1 (%2)",_display_name,(getText(configFile/"CfgVehicles"/(typeof _vehicle)/ "displayName"))];
					_list_names = _list_names + [_display_name];
				};
			} foreach _array;
		} foreach _vehicles;
	};
};
if (BTC_dlg_on_respawn > 1) then {
	_units = [];
	switch (true) do
	{
		case (BTC_dlg_on_respawn == 2) : 
		{
			_units = [leader (group player)];
		};
		case (BTC_dlg_on_respawn == 3) : 
		{
			_units = (units group player);
		};
		case (BTC_dlg_on_respawn == 4) : 
		{
			{if (_x != player && side _x == BTC_side) then { _units set [count _units, _x]; };} foreach allUnits;
		};
	};
	TRACE_1("_units",_units);
	{
		_unit = _x;
		if (!isnil "_unit"
			&& isplayer _unit
			&& {!isnull _unit} 
			&& {_unit != player}
			&& {(format ["%1",_unit getVariable ["BTC_need_revive","0"]] == "0")}) then {
			_list_names = _list_names + [name _unit];
			_list_objects = _list_objects + [_unit];
		};
	} foreach _units;
};
// TYRGHEN - Adding external possibilities
if (!isnil "tyr_fnc_execHandler") then {
	_results = ["BTC_RESPAWN_LOCATIONS",[player,_dlg_type]] call tyr_fnc_execHandler;
	{
		_location = _x;
		_list_names = _list_names + [(_location select 0)];
		_list_objects = _list_objects + [(_location select 1)];
	} foreach _results;
};

_list = [_list_names,_list_objects];
TRACE_1("End",_list);
_list